﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;

namespace deezer.sdk.windowsphone.Helpers
{
    public static class Converter
    {
        public static TypeComments ConvertJsonToTypeComments(String xmlValue)
        {
            TypeComments retour = new TypeComments();
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);

            var req = from c in xDoc.Descendants("comment")
                      select new TypeComment
                      {
                          Id = Convert.ToInt32(c.Element("id").Value),
                          Text = c.Element("text").Value,
                          Date = DateTime.Parse(c.Element("date").Value),
                          Author = (from inner in c.Elements("author")
                                    select new TypeUser
                                    {
                                        Id = Convert.ToInt32(inner.Element("id").Value),
                                        Name = inner.Element("name").Value,
                                        Link = new Uri(inner.Element("link").Value),
                                        Picture = new Uri(inner.Element("picture").Value)

                                    }).First(),
                          Type = c.Element("type").Value
                      };

            retour.Comments = req.ToList<TypeComment>();

            String tmpUri = (from c in xDoc.Descendants("next") select c.Value).First();
            retour.Next = new Uri(tmpUri);

            return retour;
        }

        public static TypeFans ConvertJsonToTypeFans(String xmlValue)
        {
            TypeFans retour = new TypeFans();
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);

            var req = from c in xDoc.Descendants("user")
                      select new TypeUser
                      {
                          Id = Convert.ToInt32(c.Element("id").Value),
                          Name = c.Element("name").Value,
                          Link = new Uri(c.Element("link").Value),
                          Picture = new Uri(c.Element("picture").Value),
                          Type = c.Element("type").Value
                      };

            retour.Users = req.ToList<TypeUser>();

            String tmpUri = (from c in xDoc.Descendants("next") select c.Value).First();
            retour.Next = new Uri(tmpUri);

            return retour;
        }

        public static TypeTracks ConvertJsonToTypeTracks(String xmlValue)
        {
            TypeTracks retour = new TypeTracks();
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);

            

            var req = from c in xDoc.Descendants("track")
                      select new TypeTrack
                      {
                          Id = Convert.ToInt32(c.Element("id").Value),
                          Readable = c.Element("readable").Value == "1" ? true : false,
                          Title = c.Element("title").Value,
                          Link = new Uri(c.Element("link").Value),
                          Duration = Convert.ToInt32(c.Element("duration").Value),
                          Rank = Convert.ToInt32(c.Element("rank").Value),
                          Preview = new Uri(c.Element("preview").Value),
                          Artist = (c.Elements("artist") != null) ?
                          (from inner in c.Elements("artist")
                                    select new TypeArtist
                                    {
                                        Id = Convert.ToInt32(inner.Element("id").Value),
                                        Name = inner.Element("name").Value,
                                    }).First() : null,
                          /*Album = (c.Element("album") != null) ?
                          (from inner in c.Elements("album")
                                    select new TypeAlbum
                                    {
                                        Id = Convert.ToInt32(inner.Element("id").Value),
                                        Title = inner.Element("title").Value,
                                        Cover = new Uri(inner.Element("cover").Value)
                                    }).First() : null,*/
                          Type = c.Element("type").Value
                      };

            retour.Tracks = req.ToList<TypeTrack>();
            return retour;
        }

        public static TypeAlbum ConvertJsonToTypeAlbum(String xmlValue)
        {
            TypeAlbum retour = new TypeAlbum();
            Int32 valueIntTemp = 0;
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);


            foreach (XElement item in xDoc.Root.Nodes())
            {
                String valueTemp = item.Name.LocalName;
                DateTime valueDateTime = DateTime.Now;
                switch (valueTemp)
                {
                    case "id":
                        if (Int32.TryParse(item.Value, out valueIntTemp))
                            retour.Id = valueIntTemp;
                        break;
                    case "title":
                        retour.Title = item.Value;
                        break;
                    case "link":
                        retour.Link = new Uri(item.Value);
                        break;
                    case "cover":
                        retour.Cover = new Uri(item.Value);
                        break;
                    case "label":
                        retour.Label = item.Value;
                        break;
                    case "duration":
                        if (Int32.TryParse(item.Value, out valueIntTemp))
                            retour.Duration = valueIntTemp;
                        break;
                    case "fans":
                        if (Int32.TryParse(item.Value, out valueIntTemp))
                            retour.Fans = valueIntTemp;
                        break;
                    case "release_date":
                        if (DateTime.TryParse(item.Value, out valueDateTime))
                            retour.Release_Date = valueDateTime;
                        break;

                    default:
                        break;
                }
            }
            //=== Artiste de l'album
            TypeArtist tempArtist = new TypeArtist();
            if (Int32.TryParse(xDoc.Root.Element("artist").Element("id").Value, out valueIntTemp))
                tempArtist.Id = valueIntTemp;
            tempArtist.Name = xDoc.Root.Element("artist").Element("name").Value;
            retour.Artist = tempArtist;
            //=============================

            //=== Tracks
            IEnumerable<XElement> xTracks = xDoc.Root.Element("tracks").Element("data").Elements("track");
            List<TypeTrack> listTracks = new List<TypeTrack>();
            foreach (XElement item in xTracks)
            {
                String temp = item.Element("id").Value;
                listTracks.Add(new TypeTrack
                {
                    Id = Convert.ToInt32(item.Element("id").Value),
                    Readable = item.Element("readable").Value == "1" ? true : false,
                    Title = item.Element("title").Value,
                    Duration = Convert.ToInt32(item.Element("duration").Value),
                    Rank = Convert.ToInt32(item.Element("rank").Value),
                    Preview = new Uri(item.Element("preview").Value),
                    Type = item.Element("type").Value
                    ,
                    Artist = new TypeArtist
                    {
                        Id = Convert.ToInt32(item.Element("artist").Element("id").Value),
                        Name = item.Element("artist").Element("name").Value
                    }
                });
            }

            retour.Tracks = listTracks;

            return retour;
        }

        public static TypeAlbums ConvertJsonToTypeAlbums(String xmlValue)
        {
            TypeAlbums retour = new TypeAlbums();

            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);

            var req = from c in xDoc.Descendants("album")
                      select new TypeAlbum
                      {
                          Id = Convert.ToInt32(c.Element("id").Value),
                          Title = c.Element("title").Value,
                          Cover = new Uri(c.Element("cover").Value),
                          GenreId = Convert.ToInt32(c.Element("").Value),
                          Type = c.Element("type").Value
                      };

            retour.Albums = req.ToList<TypeAlbum>();

            return retour;
        }

        public static TypeArtist ConvertJsonToTypeArtist(String xmlValue)
        {
            TypeArtist retour = new TypeArtist();
            Int32 valueIntTemp = 0;
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);


            foreach (XElement item in xDoc.Root.Nodes())
            {
                String valueTemp = item.Name.LocalName;
                DateTime valueDateTime = DateTime.Now;
                switch (valueTemp)
                {
                    case "id":
                        if (Int32.TryParse(item.Value, out valueIntTemp))
                            retour.Id = valueIntTemp;
                        break;
                    case "name":
                        retour.Name = item.Value;
                        break;
                    case "link":
                        retour.Link = new Uri(item.Value);
                        break;
                    case "picture":
                        retour.Picture = new Uri(item.Value);
                        break;
                    case "nb_album":
                        if (Int32.TryParse(item.Value, out valueIntTemp))
                            retour.Nb_album = valueIntTemp;
                        break;
                    case "nb_fan":
                        if (Int32.TryParse(item.Value, out valueIntTemp))
                            retour.Nb_fan = valueIntTemp;
                        break;
                    case "radio":
                        retour.Radio = item.Value == "true" ? true : false;
                        break;
                    case "type":
                        retour.Type = item.Value;
                        break;
                }
            }

            return retour;

        }

        public static List<TypeEditorial> ConvertJsonToTypeEditorial(String xmlValue)
        {
            List<TypeEditorial> retour = new List<TypeEditorial>();
            TypeEditorial retourUnit = new TypeEditorial();
            Int32 valueIntTemp = 0;
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);


            foreach (XElement item in xDoc.Root.Descendants())
            {
                String valueTemp = item.Name.LocalName;
                DateTime valueDateTime = DateTime.Now;
                switch (valueTemp)
                {
                    case "id":
                        if (Int32.TryParse(item.Value, out valueIntTemp))
                            retourUnit.Id = valueIntTemp;
                        break;
                    case "name":
                        retourUnit.Name = item.Value;
                        break;
                    case "type":
                        retourUnit.Type = item.Value;
                        break;
                }
                if (retourUnit.Id != 0 && !String.IsNullOrEmpty(retourUnit.Name) && !String.IsNullOrEmpty(retourUnit.Type))
                {
                    retour.Add(retourUnit);
                    retourUnit = new TypeEditorial();
                }
            }

            return retour;

        }

        public static List<TypeGenre> ConvertJsonToTypeGenre(String xmlValue)
        {
            List<TypeGenre> retour = new List<TypeGenre>();
            TypeGenre retourUnit = new TypeGenre();
            Int32 valueIntTemp = 0;
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);


            foreach (XElement item in xDoc.Root.Descendants())
            {
                String valueTemp = item.Name.LocalName;
                DateTime valueDateTime = DateTime.Now;
                switch (valueTemp)
                {
                    case "id":
                        if (Int32.TryParse(item.Value, out valueIntTemp))
                            retourUnit.Id = valueIntTemp;
                        break;
                    case "name":
                        retourUnit.Name = item.Value;
                        break;
                    case "type":
                        retourUnit.Type = item.Value;
                        break;
                }
                if (retourUnit.Id != 0 && !String.IsNullOrEmpty(retourUnit.Name) && !String.IsNullOrEmpty(retourUnit.Type))
                {
                    retour.Add(retourUnit);
                    retourUnit = new TypeGenre();
                }
            }

            return retour;

        }

        public static TypeResultSearch ConvertJsonToTypeResultSearch(String xmlValue)
        {
            TypeResultSearch retour = new TypeResultSearch();
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);
            //TypeTrack trackUnit = new TypeTrack();
            List<TypeTrack> listTracks = new List<TypeTrack>();

            //=== Tracks
            IEnumerable<XElement> xTracks = xDoc.Root.Element("data").Elements("track");
            foreach (XElement item in xTracks)
            {
                String temp = item.Element("id").Value;
                listTracks.Add(new TypeTrack
                {
                    Id = Convert.ToInt32(item.Element("id").Value),
                    Readable = item.Element("readable").Value == "1" ? true : false,
                    Title = item.Element("title").Value,
                    Link = new Uri(item.Element("link").Value),
                    Duration = Convert.ToInt32(item.Element("duration").Value),
                    //Rank = Convert.ToInt32(item.Element("rank").Value),
                    Preview = new Uri(item.Element("preview").Value),
                    Type = item.Element("type").Value
                    ,
                    Artist = new TypeArtist
                    {
                        Id = Convert.ToInt32(item.Element("artist").Element("id").Value),
                        Name = item.Element("artist").Element("name").Value,
                        Link = new Uri(item.Element("artist").Element("link").Value),
                        Picture = new Uri(item.Element("artist").Element("picture").Value)
                    },
                    Album = new TypeAlbum
                    {
                        Id = Convert.ToInt32(item.Element("album").Element("id").Value),
                        Title = item.Element("album").Element("title").Value,
                        Cover = new Uri(item.Element("album").Element("cover").Value)
                    }
                });
            }

            retour.Tracks = listTracks;

            return retour;
        }

        public static TypeRelated ConvertJsonToTypeRelated(String xmlValue)
        {
            TypeRelated retour = new TypeRelated();
            XDocument xDoc = XDocument.Parse(xmlValue, LoadOptions.None);

            var req = from c in xDoc.Descendants("user")
                      select new TypeArtist
                      {
                          Id = Convert.ToInt32(c.Element("id").Value),
                          Name = c.Element("name").Value,
                          Link = new Uri(c.Element("link").Value),
                          Picture = new Uri(c.Element("picture").Value),
                          Nb_album = Convert.ToInt32(c.Element("nb_album").Value),
                          Nb_fan = Convert.ToInt32(c.Element("nb_fan").Value),
                          Type = c.Element("type").Value
                      };

            retour.Artists = req.ToList<TypeArtist>();

            return retour;
        }
    }
}
